/**
 * Copyright - Accor - All Rights Reserved www.accorhotels.com
 */
package com.my.imq.web.view;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

import com.invient.vaadin.charts.InvientCharts;
import com.invient.vaadin.charts.InvientCharts.DateTimePoint;
import com.invient.vaadin.charts.InvientCharts.DateTimeSeries;
import com.my.imq.business.BrokerService;
import com.my.imq.model.Broker;
import com.my.imq.web.tools.ChartBuilder;
import com.my.imq.web.view.commons.chart.Chart;
import com.vaadin.ui.VerticalLayout;

/**
 * @author <a href="MABDELOUHAB">MABDELOUHAB</a>
 * created 12 févr. 2013
 * @since 
 * @version $Id$
 */
@Configurable(preConstruction = true)
@Component
public class BrokerLayout
    extends VerticalLayout
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private Chart splin;

    @Autowired
    private BrokerService brokerService;

    /**
     * 
     */
    public BrokerLayout()
    {

        Calendar befor = Calendar.getInstance();
        befor.add( Calendar.HOUR, -1 );
        List<Broker> brokers = brokerService.findBetweenDate( befor.getTime(), new Date() );

        ChartBuilder builder = new ChartBuilder( brokers );

        this.addComponent( builder.getGrid() );

        //        SelfUpdateSplineThread splineThread = new SelfUpdateSplineThread( splin.getChart() );
        //        splineThread.start();

    }

    private class SelfUpdateSplineThread
        extends Thread
        implements Serializable
    {
        InvientCharts chart;

        SelfUpdateSplineThread( InvientCharts chart )
        {
            this.chart = chart;
        }

        private volatile boolean keepUpdating = true;

        void stopUpdating()
        {
            keepUpdating = false;
            System.out.println( "stopUpdating " + keepUpdating );
        }

        boolean keepUpdating()
        {
            return keepUpdating;
        }

        public void run()
        {
            while ( keepUpdating() )
            {
                try
                {
                    // Sleep for 1 second
                    Thread.sleep( 10000 );
                }
                catch ( InterruptedException e )
                {
                    System.out.println( "InterruptedException occured. Exception message " + e.getMessage() );
                }
                synchronized ( getApplication() )
                {
                    DateTimeSeries seriesData1 = (DateTimeSeries) chart.getSeries( "numMsgsIn" );
                    DateTimeSeries seriesData2 = (DateTimeSeries) chart.getSeries( "numMsgsOut" );
                    Calendar befor = Calendar.getInstance();
                    befor.add( Calendar.SECOND, -10 );

                    List<Broker> brokers = brokerService.findBetweenDate( befor.getTime(), new Date() );
                    for ( Broker broker : brokers )
                    {
                        seriesData1.addPoint( new DateTimePoint( seriesData1, broker.getTime(), Double
                            .parseDouble( broker.getNumMsgsIn() ) ) );
                        seriesData2.addPoint( new DateTimePoint( seriesData2, broker.getTime(), Double
                            .parseDouble( broker.getNumMsgsOut() ) ) );
                    }

                }
                System.out.println( "Inside run() keepUpdating " + keepUpdating );
            }
        }

    }

}
